function getMax(data)
  local key=1;
  local maxKey=1;
  local maxVal=0;

  for w in string.gmatch(data, "%d*") do
    w = tonumber(w);
    if w > maxVal then
      maxVal = w
      maxKey = key;
    end

    key = key + 1;
  end

  return maxKey, maxVal;
end

function getObject(data, maxIndex)
  local key=1;

  for id, val in string.gmatch(data, "id=\"(.-)\">(.-)</") do
    if key == maxIndex then
      return id, val;
    end

    key = key + 1;
  end
end

data = [[
<?xml version='1.0' encoding='gb2312' standalone='yes' ?><nlp>
  <version>1.1</version>
  <rawtext>不需要位置价格配套购买贷款期房房产证开放商交通</rawtext>
  <confidence>60</confidence>
  <engine>local</engine>
  <result>
    <focus>no|ask1|ask2|ask3|ask4|ask5|ask6|ask7|ask8|ask9</focus>
    <confidence>21|51|95|64|100|99|76|46|64|87</confidence>
    <object>
      <no id="17001">不需要</no>
      <ask1 id="18001">位置</ask1>
      <ask2 id="18002">价格</ask2>
      <ask3 id="18003">配套</ask3>
      <ask4 id="18006">购买</ask4>
      <ask5 id="18004">贷款</ask5>
      <ask6 id="18008">期房</ask6>
      <ask7 id="18009">房产证</ask7>
      <ask8 id="18010">开放商</ask8>
      <ask9 id="18011">交通</ask9>
    </object>
  </result>
</nlp>
]]

local _,_,confidence = string.find(data, "</focus>.-<confidence>(.-)</confidence>")
local maxIndex, maxVal = getMax(confidence);

local objId, objVal = getObject(data, maxIndex);
print("final Id:" .. objId)
print("final val:" .. objVal)
